/**
 * Created with Intellij IDEA.
 * Description;
 * User:墨琦
 * Data:2023-09-20
 * Time:16:56
 */
public class Text1 {
    //n²
    public int minSubArrayLen1(int target, int[] nums) {
        int num=Integer.MAX_VALUE;
        for(int i=0;i<nums.length;i++)
        {
            if(nums[i]>=target)
            {
                num=1;
                break;
            }
            int count=target-nums[i];
            for(int j=i+1;j<nums.length;j++)
            {
                if(num<j-i+1)
                {
                    break;
                }
                else {
                    count-=nums[j];
                    if(count<=0)
                    {
                        num=j-i+1;
                    }
                }
            }
        }
        return num!=Integer.MAX_VALUE?num:0;
    }
    //n
    public static int minSubArrayLen2(int target, int[] nums){
        int n=nums.length,sum=0,len=Integer.MAX_VALUE;
        for(int left=0,right=0;right<n;right++)
        {
            sum+=nums[right];
            while(sum<target)
            {
                len=Math.min(len,right-left+1);
                sum-=nums[left++];
            }
        }
        return len==Integer.MAX_VALUE?0:len;
    }

    public static void main(String[] args) {
        int a=minSubArrayLen2(7,new int[]{2,3,1,2,4,3});
        System.out.println(a);
    }

}
